package com.kedacom.freedomLabel.view.activity.report;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.kedacom.freedomLabel.R;
import com.kedacom.freedomLabel.base.BaseActivity;
import com.kedacom.freedomLabel.databinding.ActivityReportReportSummaryBinding;
import com.kedacom.freedomLabel.viewmodel.report.ReportSummaryViewModel;
import com.kedacom.lego.annotation.ContentView;
import com.kedacom.lego.annotation.ViewModel;

/**
 * 汇总报表
 */

@ContentView(R.layout.activity_report_report_summary)
@ViewModel(ReportSummaryViewModel.class)
public class ReportSummaryActivity extends BaseActivity<ActivityReportReportSummaryBinding, ReportSummaryViewModel> {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onSelectReportType(0); // 默认为日报表
        mBinding.dailyReport.setOnClickListener(v -> {
            beep();
            onSelectReportType(0);
        });
        mBinding.monthReport.setOnClickListener(v -> {
            beep();
            onSelectReportType(1);
        });
        mBinding.productReport.setOnClickListener(v -> {
            beep();
            onSelectReportType(2);
        });

        mBinding.operatorReport.setOnClickListener(v->{
            beep();
            onSelectReportType(3);
        });

        mViewModel.reportType.observe(this, type -> {
            if (type == null)
                return;
            ReportSummaryFragment reportSummaryFragment = new ReportSummaryFragment();
            ReportSummaryFragment.type = type;
            getSupportFragmentManager().popBackStackImmediate();
            getSupportFragmentManager().beginTransaction().replace(R.id.report_container, reportSummaryFragment).commit();
        });
    }

    private void onSelectReportType(int type) {
        if (mViewModel.reportType.getValue() != null
                && mViewModel.reportType.getValue() == type)
            return;
        mBinding.dailyReport.setBackgroundColor(ContextCompat.getColor(this, type == 0 ? R.color.colorPrimary : R.color.colorPrimaryDark));
        mBinding.monthReport.setBackgroundColor(ContextCompat.getColor(this, type == 1 ? R.color.colorPrimary : R.color.colorPrimaryDark));
        mBinding.productReport.setBackgroundColor(ContextCompat.getColor(this, type == 2 ? R.color.colorPrimary : R.color.colorPrimaryDark));
        mBinding.operatorReport.setBackgroundColor(ContextCompat.getColor(this, type == 3 ? R.color.colorPrimary : R.color.colorPrimaryDark));
        mViewModel.reportType.postValue(type);
    }

    @Override
    public ReportSummaryViewModel getViewModel() {
        return ViewModelProviders.of(this).get(ReportSummaryViewModel.class);
    }


    public void onDetailClick(View view) {
        beep();
        Intent intent = new Intent(view.getContext(), ReportActivity.class);
        startActivity(intent);
    }

    public void clickBack(View view) {
        beep();
        finish();
    }
}
